﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Stanza.MSBuild.Tasks
{
    public class SolutionFileHandler : FileHandler
    {
        public SolutionFileHandler( PublishInfo pubInfo )
            : base( pubInfo )
        {
        }

        public override void ParseFile( string fileName )
        {
            Info.AddPublishFile( new PublishFile { FileName = fileName, ModifyFile = UpdateSolutionFile } );

            string solutionText = ReadSourceFileText( fileName );
            string solutionPath = Path.GetDirectoryName( fileName );

            // Find the projects:
            //
            //     Project("{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}") = "ProjectName", "Path\ProjectName.csproj", "{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"
            //
            Regex projRegex = new Regex( @"Project\(""\{(?<PrjType>[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})\}""\)" +
                                         @"\s*=\s*""(?<PrjName>.*?)""," +
                                         @"\s*""(?<PrjFile>.*?)""," +
                                         @"\s*""\{(?<PrjGUID>[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12})\}""",
                                         RegexOptions.Singleline | RegexOptions.IgnoreCase );

            foreach( Match m in projRegex.Matches( solutionText ) )
            {
                string prjType = m.Groups["PrjType"].Value;
                string prjName = m.Groups["PrjName"].Value;
                string prjFile = m.Groups["PrjFile"].Value;
                string prjGUID = m.Groups["PrjGUID"].Value;

                string fullFileName = Path.Combine( solutionPath, prjFile );
                switch( prjType.ToUpper() )
                {
                case "FAE04EC0-301F-11D3-BF4B-00C04F79EFBC":        // C# Project File Type
                    ProjectFileHandler csProject = new ProjectFileHandler( Info );
                    csProject.ParseFile( fullFileName );
                    break;
                }
            }
        }

        private Stream UpdateSolutionFile( Stream readStream )
        {
            Encoding streamEncoding;
            string solutionText;
            using( StreamReader textReader = new StreamReader( readStream ) )
            {
                streamEncoding = textReader.CurrentEncoding;
                solutionText = textReader.ReadToEnd();
            }

            // Remove Team Foundation Server settings:
            //
            //     GlobalSection(TeamFoundationVersionControl) = preSolution
            //          ...
            //     EndGlobalSection
            //
            Regex tfsRegex = new Regex( @"\n\s*GlobalSection\(TeamFoundationVersionControl\).*?EndGlobalSection\s*?\n",
                                        RegexOptions.Singleline | RegexOptions.IgnoreCase );
            solutionText = tfsRegex.Replace( solutionText, "\n" );

            return SaveSourceFileText( solutionText, streamEncoding );
        }
    }
}
